﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class ServicesSheet_UseCarList : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dtpUDate.Text = DateTime.Now.ToShortDateString();
            dtpUDateTo.Text = DateTime.Now.ToShortDateString();
            InitializeComponent();
             Master.DataSource = WMaster;

            MasterPage_MasterPage master = (MasterPage_MasterPage)this.Page.Master;
            master.HelpHref = "~/Help/ServicesSheet/用车申请查询.htm";
            master.PageName = "公司车辆使用查询";
            master.PicUrl = "";
            master.IntiBar();
            SetDatabase(false);
        }
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ServicesSheet_UseCarList));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = true;
        this.WMaster.AlwaysClose = true;
        this.WMaster.DataCompressed = false;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "ServiceSheet.View_Services_UseCars";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }
    protected void WebNavigator1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "cmdPrint")
        {
            string whereIn = "(";
            for (int i = 0; i < this.wgvMaster.Rows.Count; i++)
            {
                CheckBox cbPrint = (CheckBox)this.wgvMaster.Rows[i].FindControl("CheckBox1");
                if (cbPrint.Checked == true)
                {
                    string usecarId = this.wgvMaster.Rows[i].Cells[2].Text;
                    whereIn += usecarId + ",";

                }
            }
            if (whereIn == "(")
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('请在要打印的数据前面打钩！')", true);
                return;
            }
            if (whereIn.EndsWith(","))
            {
                whereIn = whereIn.Remove(whereIn.Length - 1);
            }
            whereIn += ")";
            this.Response.Redirect("UseCarPrint.aspx?printId=" + whereIn);

        }
        if (e.CommandName == "cmdAdd")
        {
            Response.Redirect("UseCar.aspx?FLOWFILENAME=FL\\ApplicateCar&NAVMODE=Insert&FLNAVMODE=Submit");//&NAVMODE=Inset&FLNAVMODE=Submit
        }
        else if (e.CommandName == "cmdClientQuery")
        {
            string ss = this.ResolveUrl("..\\InnerPages\\FlowDesigner.aspx") + "?FLOWFILENAME=ApplicateCar";
            Response.Write(" <script   language=javascript> window.open( '" + ss + " ','流程图','height=600,width=800,top=50,left=100,toolbar=no,menubar=no,scrollbars=yes, resizable=yes,location=no, status=no'); </script> ");
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        InitializeComponent();
        SetDatabase(true);
        Master.DataSource = WMaster;
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {

        this.QueryCars.BindingText = "";
        this.QueryCars.BindingValue = "";
        // this.dtpUDate.Text = "";


        InitializeComponent();
        SetDatabase(true);
        Master.DataSource = WMaster;
    }

    private void SetDatabase(bool pIsPostBack)
    {
        string where = "";

        string strCarId = this.QueryCars.BindingValue;
        string strDate = this.dtpUDate.Text;
        string strDateTo = this.dtpUDateTo.Text;

        //非管理员只能查看自已的单据
        if (Srvtools.CliUtils.fGroupID.IndexOf(System.Configuration.ConfigurationManager.AppSettings["FlowAdmin"].ToString()) == -1)
        {
            String wh = " Services_UseCars.EmployeeId='" + Convert.ToInt32(Srvtools.CliUtils.fLoginUser).ToString() + "'";
               where = wh;
          }
        else
        {
            where = " (1=1) ";
        }
        if (strCarId == string.Empty) strCarId = "";
        if (pIsPostBack == true)
        {
            if (strDate == string.Empty) strDate = "";
            if (strDateTo == string.Empty) strDateTo = "";
        }
        else
        {
            if (strDate == string.Empty || strDate == "")
            {
                strDate = DateTime.Now.ToShortDateString();
            }
            if (strDateTo == string.Empty || strDateTo == "")
            {
                strDateTo = DateTime.Now.ToShortDateString();
            }

        }

        if (strCarId.Length > 0)
            where += " AND Services_UseCars.CarId=" + strCarId;
        if (strDate.Length > 0)
            where += " AND Services_UseCars.UCDate >='" + strDate + " 0:0:0'";
        if (strDateTo.Length > 0)
            where += " AND Services_UseCars.UCDate <='" + strDateTo + " 23:59:59'";
        this.WMaster.SetWhere(where);

    }

    protected void wgvMaster_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "del")
        {
            string[] c = e.CommandArgument.ToString().Split(',');
            /*
             * 0、table 
             * 1、PK
             * 2、id
             * 3、WorkTime_AddUp where 
             * 4、WorkTime_OverTime2Rest where 
             * 5、WorkTime_YearHoliday where
             */
            object[] objParam = new object[]{"Services_UseCars","UseCarId",e.CommandArgument.ToString()," 1=0" , " 1=0", " 1=0"};
            object[] myRet = Srvtools.CliUtils.CallMethod("SWorkTime", "InvalidWorkOnForm", objParam);
            ClientScript.RegisterStartupScript(GetType(), "", "alert('作废成功！');location.href='" + Request.Url.ToString() + "'", true);
        }
    }

    protected void wgvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            ImageButton imb = (ImageButton)e.Row.FindControl("del");

            if (drv["FlowFlag"].ToString() == "X" || drv["FlowFlag"].ToString() == "")//&&
            // Srvtools.CliUtils.fGroupID.IndexOf(System.Configuration.ConfigurationManager.AppSettings["FlowAdmin"].ToString()) != -1)
            {
                imb.CommandArgument = drv["UseCarId"].ToString();
                imb.CommandName = "del";
                imb.Attributes.Add("onclick", "return confirm('确认作废请假单" + drv["UseCarId"].ToString() + " ？')");
            }
            else
            {
                imb.Visible = false;
            }
        }

    }
}
